package com.datas.service;

import com.datas.db.hibernatebeans.UmsRole_UmsPageResource;
import com.datas.db.hibernatebeans.UmsUser_UmsDepartment;
import com.datas.db.hibernatebeans.UmsUser_UmsDepartment_mng;
import com.datas.db.hibernatebeans.UmsUser_UmsRole;
import com.datas.utils.hbn.HbnTxRuner;
import org.hibernate.Session;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by jige0727 on 2017/5/28.
 * 关联设置
 */
@Service
public class GuanlianService {


//    public Integer getGL_role_pageresource(Integer roleid) {
//        return (Integer) new HbnTxRuner() {
//            @Override
//            protected Object dataLogic() throws Exception {
//                return getGL_role_pageresource(roleid, getCurrentSession());
//            }
//        }.runTx();
//    }
//
//    public Integer getGL_role_pageresource(Integer roleid, Session session) {
//        session.createQuery("select i from UmsPageResource i " +
//                "inner join UmsRole_UmsPageResource rp on rp.pageresources_id = i.id " +
//                "where rp.roles_id=:roles_id ")
//                .setParameter("roles_id", roleid).list();
//    }

    /**
     * 设置角色和部门的关联
     *
     * @param userid
     * @param deptids
     * @return
     */
    public Integer setGL_user_department(Integer userid, Collection<Integer> deptids) {
        return (Integer) new HbnTxRuner() {
            @Override
            protected Object dataLogic() throws Exception {
                fullTreeIds_dept(deptids, getCurrentSession());
                return setGL_user_department(userid, deptids, getCurrentSession());
            }
        }.runTx();
    }

    /**
     * 设置角色和管理部门的关联
     *
     * @param userid
     * @param deptids
     * @return
     */
    public Integer setGL_user_mng_department(Integer userid, Collection<Integer> deptids) {
        return (Integer) new HbnTxRuner() {
            @Override
            protected Object dataLogic() throws Exception {
                fullTreeIds_dept(deptids, getCurrentSession());
                return setGL_user_mng_department(userid, deptids, getCurrentSession());
            }
        }.runTx();
    }

    private void fullTreeIds_dept(Collection<Integer> deptids, Session session) {
        if (deptids.isEmpty()) {
            deptids.add(-100);
        }
        Set<Integer> ids = new HashSet<>();
        ids.addAll(deptids);
        List<String> structs = session.createQuery("select i.struct from UmsDepartment i " +
                "where i.parent_dept_id is not null and i.id in (:deptids) ")
                .setParameterList("deptids", deptids).list();
        for (String struct : structs) {
            for (String idStr : struct.split("-")) {
                ids.add(Integer.parseInt(idStr));
            }
        }
        deptids.clear();
        deptids.addAll(ids);
        deptids.remove(-100);
    }

    public Integer setGL_user_department(Integer userid, Collection<Integer> deptids, Session session) {
        String hql = "delete from UmsUser_UmsDepartment where users_id=:userid ";
        session.createQuery(hql)
                .setParameter("userid", userid)
                .executeUpdate();
        session.flush();
        for (Integer deptid : deptids) {
            session.save(new UmsUser_UmsDepartment(userid, deptid));
        }
        return 1;
    }
    public Integer setGL_user_mng_department(Integer userid, Collection<Integer> deptids, Session session) {
        String hql = "delete from UmsUser_UmsDepartment_mng where users_id=:userid ";
        session.createQuery(hql)
                .setParameter("userid", userid)
                .executeUpdate();
        session.flush();
        for (Integer deptid : deptids) {
            session.save(new UmsUser_UmsDepartment_mng(userid, deptid));
        }
        return 1;
    }

    /**
     * 角色和资源的关联
     *
     * @param roleid
     * @param pageresids
     * @return
     */
    public Integer setGL_role_pageresource(Integer roleid, Collection<Integer> pageresids) {
        return (Integer) new HbnTxRuner() {
            @Override
            protected Object dataLogic() throws Exception {
                fullTreeIds_pageresource(pageresids, getCurrentSession());
                return setGL_role_pageresource(roleid, pageresids, getCurrentSession());
            }
        }.runTx();
    }

    private void fullTreeIds_pageresource(Collection<Integer> pageresids, Session session) {
        if (pageresids.isEmpty()) {
            pageresids.add(-100);
        }
        Set<Integer> ids = new HashSet<>();
        ids.addAll(pageresids);
        List<String> structs = session.createQuery("select i.struct from UmsPageResource i " +
                "where i.parent_rese_id is not null and i.id in (:pageresids) ")
                .setParameterList("pageresids", pageresids).list();
        for (String struct : structs) {
            for (String idStr : struct.split("-")) {
                ids.add(Integer.parseInt(idStr));
            }
        }
        pageresids.clear();
        pageresids.addAll(ids);
        pageresids.remove(-100);
    }

    public Integer setGL_role_pageresource(Integer roleid, Collection<Integer> pageresids, Session session) {
        String hql = "delete from UmsRole_UmsPageResource where roles_id=:roles_id ";
        session.createQuery(hql)
                .setParameter("roles_id", roleid)
                .executeUpdate();
        session.flush();
        for (Integer pageresid : pageresids) {
            session.save(new UmsRole_UmsPageResource(roleid, pageresid));
        }
        return 1;
    }

    /**
     * 用户和角色的关联
     *
     * @param userid
     * @param roleids
     * @return
     */
    public Integer setGL_user_role(Integer userid, Collection<Integer> roleids) {
        return (Integer) new HbnTxRuner() {
            @Override
            protected Object dataLogic() throws Exception {
                return setGL_user_role(userid, roleids, getCurrentSession());
            }
        }.runTx();
    }

    public Integer setGL_user_role(Integer userid, Collection<Integer> roleids, Session session) {
        String hql = "delete from UmsUser_UmsRole where users_id=:users_id ";
        session.createQuery(hql)
                .setParameter("users_id", userid)
                .executeUpdate();
        session.flush();
        for (Integer roleid : roleids) {
            session.save(new UmsUser_UmsRole(userid, roleid));
        }
        return 1;
    }


    /**
     * 删除关联 全部关联
     *
     * @param pageResourceId
     * @return
     */
    public Integer delGl_all_pageresource(Integer pageResourceId, Session session) {
        Integer res = 0;
        res += session.createQuery("delete from UmsDepartment_UmsPageResource " +
                "where pageresources_id=:pageresources_id")
                .setParameter("pageresources_id", pageResourceId)
                .executeUpdate();

        res += session.createQuery("delete from UmsRole_UmsPageResource " +
                "where pageresources_id=:pageresources_id")
                .setParameter("pageresources_id", pageResourceId)
                .executeUpdate();

        res += session.createQuery("delete from UmsUser_UmsPageResource " +
                "where pageresources_id=:pageresources_id")
                .setParameter("pageresources_id", pageResourceId)
                .executeUpdate();
        return res;
    }

    /**
     * 删除关联 部门的全部关联
     *
     * @param deptId
     * @return
     */
    public Integer delGl_all_dept(Integer deptId, Session session) {
        Integer res = 0;
        res += session.createQuery("delete from UmsUser_UmsDepartment " +
                "where departments_id=:departments_id")
                .setParameter("departments_id", deptId)
                .executeUpdate();

        res += session.createQuery("delete from UmsDepartment_UmsPageResource " +
                "where departments_id=:departments_id")
                .setParameter("departments_id", deptId)
                .executeUpdate();

        return res;
    }

    /**
     * 删除关联 角色的全部关联
     *
     * @param roleId
     * @return
     */
    public Integer delGl_all_role(Integer roleId, Session session) {
        Integer res = 0;
        res += session.createQuery("delete from UmsUser_UmsRole " +
                "where roles_id=:roles_id")
                .setParameter("roles_id", roleId)
                .executeUpdate();

        res += session.createQuery("delete from UmsRole_UmsPageResource " +
                "where roles_id=:roles_id")
                .setParameter("roles_id", roleId)
                .executeUpdate();

        return res;
    }

    /**
     * 删除关联 账号的全部关联
     *
     * @param usetId
     * @return
     */
    public Integer delGl_all_user(Integer usetId, Session session) {
        Integer res = 0;
        res += session.createQuery("delete from UmsUser_UmsDepartment " +
                "where users_id=:users_id")
                .setParameter("users_id", usetId)
                .executeUpdate();

        res += session.createQuery("delete from UmsUser_UmsPageResource " +
                "where users_id=:users_id")
                .setParameter("users_id", usetId)
                .executeUpdate();

        res += session.createQuery("delete from UmsUser_UmsRole " +
                "where users_id=:users_id")
                .setParameter("users_id", usetId)
                .executeUpdate();

        return res;
    }
}
